
/**   
 * @Title: DateUtil.java 
 * @Package: com.rename.icode.utils 
 * @Description: Administrator版权所有
 * @author Administrator  
 * @date 2015-2-11 下午4:43:20 
 * @version  
 */


package com.rename.icode.utils;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/** 
 * @Description 实现了...功能
 * @author Administrator
 * @date 2015-2-11 下午4:43:20 
 * @version V1.0
 */

public class DateUtil {
	public static final String FORMAT = "yyyy-MM-dd HH:mm:ss SSS";
	public static final String SIMPLE_FORMAT = "yyyy-MM-dd HH:mm:ss";

	public static Date str2Date(String str) {
		return str2Date(str, null);
	}

	public static Date str2Date(String str, String format) {
		if (str == null || str.length() == 0) {
			return null;
		}
		if (format == null || format.length() == 0) {
			format = FORMAT;
		}
		Date date = null;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			date = sdf.parse(str);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return date;

	}

	public static Calendar str2Calendar(String str) {
		return str2Calendar(str, null);

	}

	public static Calendar str2Calendar(String str, String format) {

		Date date = str2Date(str, format);
		if (date == null) {
			return null;
		}
		Calendar c = Calendar.getInstance();
		c.setTime(date);

		return c;

	}

	public static String date2Str(Calendar c) {// yyyy-MM-dd HH:mm:ss
		return date2Str(c, null);
	}

	public static String date2Str(Calendar c, String format) {
		if (c == null) {
			return null;
		}
		return date2Str(c.getTime(), format);
	}

	public static String date2Str(Date d) {// yyyy-MM-dd HH:mm:ss
		return date2Str(d, null);
	}

	public static String date2Str(Date d, String format) {// yyyy-MM-dd HH:mm:ss
		if (d == null) {
			return null;
		}
		if (format == null || format.length() == 0) {
			format = FORMAT;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		String s = sdf.format(d);
		return s;
	}

	public static String getCurDateStr() {
		Calendar c = Calendar.getInstance();
		return date2Str(c);
	}
	
	public static String getCurDateLong() {
		Calendar c = Calendar.getInstance();
		return c.getTimeInMillis()+"";
	}
	//modify log:bug-fix:#1818.--OF
	/**
	 * Get BeiJing Time.
	 * @return long.
	 */
	@SuppressWarnings("unused")
	public static long getInternetTimeLong() {
		
		  java.util.Locale locale=java.util.Locale.CHINA; 
		  long ld = 0;
		  java.net.URL url = null;
		try {
			url = new URL("http://www.bjtime.cn");
		} catch (MalformedURLException e) {
			
			e.printStackTrace();
		}
		  java.net.URLConnection uc = null;
		try {
			uc = url.openConnection();
		} catch (IOException e) {
			
			e.printStackTrace();
		}
		  try {
			uc.connect();
			ld =uc.getDate();
		} catch (IOException e) {
			e.printStackTrace();
		} 
		return ld;
	}

	/**
	 * �����ゆ�风�����瑙�甯���烽����ゆ�疯�������ゆ�峰�������ゆ�烽����ゆ�烽��缁�锟�
	 * 
	 * @param format
	 * @return
	 */
	public static String getCurDateStr(String format) {
		Calendar c = Calendar.getInstance();
		return date2Str(c, format);
	}

	// �����ゆ�峰�������ゆ�烽����ゆ��
	public static String getMillon(long time) {

		return new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(time);

	}

	// �����ゆ�峰�������ゆ�烽����ゆ��
	public static String getDay(long time) {

		return new SimpleDateFormat("yyyy-MM-dd").format(time);

	}

	// �����ゆ�峰�������ゆ�烽����ゆ�烽����ゆ��
	public static String getSMillon(long time) {

		return new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss-SSS").format(time);

	}
	
//	public static String getMDHM(long time) {
//		String format = ApplicationContext.get().getResources().getString(R.string.date_format);
//		return new SimpleDateFormat(format).format(time);
//	}
//	public static String getCallDur(long time){
//		if(time>60&&time<3600){
//			String format = ApplicationContext.get().getResources().getString(R.string.min_format);
//			return new SimpleDateFormat(format).format(time*1000);
//		}else if(time>=3600){
//			String format = ApplicationContext.get().getResources().getString(R.string.hour_format);
//			return new SimpleDateFormat(format).format(time*1000);
//		}else{
//			return time+ApplicationContext.get().getResources().getString(R.string.x_seconds);
//		}
//	}
}
